package com.kingsalert.kingsalert.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.kingsalert.kingsalert.R;


public class LoadingDialog extends Dialog {

    private final FragmentActivity activity;

    public LoadingDialog(@NonNull FragmentActivity activity) {
        super(activity);
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public FragmentActivity getActivity() {
        return activity;
    }

    @Override
    public void show() {
        super.show();
        new Thread(() -> {
            try {
                Thread.sleep(5000);
                if (activity!=null && isShowing())
                    activity.runOnUiThread(this::hide);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }

}
